package com.fyx.timedTask;


import com.alipay.api.internal.util.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.FiveMongoConfig;
import com.fyx.config.NinethMongoConfig;
import com.fyx.config.SecondaryMongoConfig;
import com.fyx.entity.*;

import com.fyx.mapper.*;

import com.fyx.until.DateUtil;
import com.fyx.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.mapstruct.ap.shaded.freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationExpression;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.security.rsa.RSASignature;

import javax.annotation.Resource;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.List;

@Slf4j
@Component(value="BankInfoTask")
public class BankInfoTask {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = SecondaryMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate secondaryMongoTemplate;

    @Resource(name = FiveMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate fiveMongoTemplate;

    @Resource(name = NinethMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate ninethMongoTemplate;

    @Autowired
    private MeituanReCountMapper meituanReCountMapper;

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    @Autowired
    private GoldRecCountMapper goldRecCountMapper;

    @Autowired
    private MatchTicketInfoMapper matchTicketInfoMapper;

    public void run(){
        Long beginTime=DateUtil.getDayBegin().getTime()/1000;
        long nowTime=new Date().getTime()/1000;
        String tableName="props_rec_"+DateUtil.timeStamp2Date(beginTime+"","yyyyMM");;
        if(beginTime==nowTime){
            beginTime=DateUtil.getBeginDayOfYesterday().getTime()/1000;
            if(!DateUtil.timeStamp2Date(beginTime+"","yyyyMM").equals(DateUtil.timeStamp2Date(nowTime+"","yyyyMM"))){
                tableName ="props_rec_"+DateUtil.timeStamp2Date(beginTime+"","yyyyMM");
            }
        }
        long endTime=beginTime+86400;

        record(beginTime,endTime);
        getRecordGold(beginTime,"total");
        getRecordGold(beginTime,"totalcl");
        getRecordGold(beginTime,"totalapp");
        recordTicket( beginTime, 100208);
        recordTicket( beginTime, 100206);
        log.info("美团人数记录任务：执行完毕=======================");
    }


    private void record(Long beginTime,Long endTime){
        Integer[] s={0,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000};
        Integer[] g={6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000,999999};
        String str="";
        for(int i=0;i<26;i++) {
            Criteria criteria = new Criteria();
            criteria.and("channel").is("h5hzmjxl_meituan");
            criteria.and("backpack").elemMatch(Criteria.where("id").is(100126).and("num").gte(s[i]).lt(g[i]));
            Long tatol=0l;
            try {
                tatol=mongoTemplate.count(new Query().addCriteria(criteria),"user");

            }catch (Exception e){   }
            criteria = new Criteria();
            criteria.and("channel").is("h5hzmjxl_meituan_re");
            criteria.and("backpack").elemMatch(Criteria.where("id").is(100126).and("num").gte(s[i]).lt(g[i]));
            try {
                tatol+=mongoTemplate.count(new Query().addCriteria(criteria),"user");
            }catch (Exception e){   }
            str+=s[i]+"-"+g[i]+"@"+tatol+"#";

        }
        MeituanReCount meituanReCount =new MeituanReCount();
        meituanReCount.setTime(beginTime.intValue());
        meituanReCount.setPerson(str);
        System.out.println(str);
        QueryWrapper<MeituanReCount> wrapper=new QueryWrapper<>();
        wrapper.eq("time",beginTime);
        MeituanReCount meituanReCount1=meituanReCountMapper.selectOne(wrapper);
        if(meituanReCount1==null){
            meituanReCountMapper.insert(meituanReCount);
        }
        else {
            meituanReCountMapper.update(meituanReCount,wrapper);
        }
    }

    private void getRecordGold(Long beginTime,String channel){
        String[] str=new String[18];
        String strLost="";
        str[0]="0";str[1]="1@50000000";str[2]="50000000@500000000";
        str[3]="500000000@5000000000";str[4]="5000000000@50000000000";str[5]="50000000000@100000000000";
        str[6]="100000000000@500000000000";str[7]="500000000000@1000000000000";str[8]="1000000000000@5000000000000";
        str[9]="5000000000000@50000000000000";str[10]="50000000000000@100000000000000";str[11]="100000000000000@500000000000000";
        str[12]="500000000000000@1000000000000000";str[13]="1000000000000000@5000000000000000";str[14]="5000000000000000@10000000000000000";
        str[15]="10000000000000000@50000000000000000";str[16]="50000000000000000@500000000000000000";str[17]="500000000000000000";
        GoldRecCount goldRecCount=new GoldRecCount();
        goldRecCount.setChannel(channel);
        goldRecCount.setTime(beginTime.intValue());
            for(int i=0;i<str.length;i++){
                Criteria criteria=new Criteria();
                if(i==0){
                  criteria.and("gold").is(0);
                } else if(i==17){
                    criteria.and("gold").gt(Long.parseLong(str[i]));
                }else {
                    criteria.and("gold").gte(Long.parseLong(str[i].split("@")[0])).lt(Long.parseLong(str[i].split("@")[1]));
                }
                Query query=new Query();
                query.addCriteria(criteria);
                if(channel.equals("total")) {
                    strLost += mongoTemplate.count(query, "user") + ",";
                }else if(channel.equals("totalcl")){
                    strLost += fiveMongoTemplate.count(query, "user") + ",";
                }else {
                    strLost += ninethMongoTemplate.count(query, "user") + ",";
                }
            }
        goldRecCount.setGoldCount(strLost);
        QueryWrapper<GoldRecCount> wrapper=new QueryWrapper<>();
        wrapper.eq("time",beginTime);
        wrapper.eq("channel",goldRecCount.getChannel());
        GoldRecCount goldRecCount1=goldRecCountMapper.selectOne(wrapper);
        if(goldRecCount1==null){
            goldRecCountMapper.insert(goldRecCount);
        }
        else {
            goldRecCountMapper.update(goldRecCount,wrapper);
        }
    }

    private void recordTicket(Long beginTime,Integer idStr){
        Integer[] s={1,10,50,100,500,1000,5000,10000};
        Integer[] g={10,50,100,500,1000,5000,10000,50000};
        String str="";
        MatchTicketInfo matchTicketInfo=new MatchTicketInfo();
        matchTicketInfo.setTime(beginTime.intValue());
        if(idStr==100208){
            matchTicketInfo.setFlag("ticket");
        }else {
            matchTicketInfo.setFlag("lottery");
        }
        for (int i = 0; i < (s.length+1); i++) {
            Criteria criteria = new Criteria();
            if (i == 8) {
                criteria.and("backpack").elemMatch(Criteria.where("id").is(idStr).and("num").gte(50000));
            } else {
                criteria.and("backpack").elemMatch(Criteria.where("id").is(idStr).and("num").gte(s[i]).lt(g[i]));
            }
            try {
                str +=mongoTemplate.count(new Query().addCriteria(criteria),"user")+",";

            }catch (Exception e){   }
        }
        matchTicketInfo.setSettlement(str);
        QueryWrapper<MatchTicketInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("time",beginTime);
        wrapper.eq("flag",matchTicketInfo.getFlag());
        MatchTicketInfo matchTicketInfo1=matchTicketInfoMapper.selectOne(wrapper);
        if(matchTicketInfo1==null){
            matchTicketInfoMapper.insert(matchTicketInfo);
        }
        else {
            matchTicketInfoMapper.update(matchTicketInfo,wrapper);
        }
    }
}
